﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Text.RegularExpressions;
using System.IO;

namespace WOWMultiPlay
{
    public static class Helper_INIFile
    {
        public static void IniWriteValue(string Section, string Key, string Value, string FilePath)
        {
            Helper_Win32API.WritePrivateProfileString(Section, Key, Value, FilePath);
        }

        public static string IniReadValue(string Section, string Key, string FilePath)
        {
            StringBuilder strB = new StringBuilder(255);
            int i = Helper_Win32API.GetPrivateProfileString(Section, Key, "", strB, 255, FilePath);
            return strB.ToString();
        }

        public static string[] IniGetAllSections(string FilePath)
        {
            StreamReader SR = new StreamReader(FilePath);
            string IniContents = SR.ReadToEnd();
            SR.Close();

            Regex IniSectionsRegex = new Regex("\\[((?:.|\n)*?)\\]");
            MatchCollection IniSectionsMatches = IniSectionsRegex.Matches(IniContents);

            if (IniSectionsMatches.Count > 0)
            {
                string[] AllSections = new string[IniSectionsMatches.Count];

                for (int i = 0; i < IniSectionsMatches.Count; i++)
                {
                    AllSections[i] = IniSectionsMatches[i].Groups[1].Value;
                }

                return AllSections;
            }
            else
            {
                return new string[0];
            }
        }

        public static void CreateINIFile(string FilePath)
        {
            StreamWriter SW = File.CreateText(FilePath);
            SW.Close();
        }
    }
}